package cn.com.guage.user.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import cn.com.guage.user.entity.ProvinceEntity;
import cn.com.guage.user.entity.ProvinceEntity2;
import cn.com.guage.user.entity.User;
/**
 * 
 * @author yangdechao
 *
 */
@RestController
public class UserController {

//    private final static Logger logger = LoggerFactory.getLogger("Application_ERROR");
	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@GetMapping("/user0")
	public String user0() {
		logger.info("开始执行selectUserFromDB方法---------------------");
		User user = new User();
		user.setName("小红");
		user.setAge(15);
		user.setSex("男");
		logger.info("selectUserFromDB查询用户信息为：" + user);
		logger.info("selectUserFromDB查询用户信息为：" + JSON.toJSONString(user));
		try {
			int a = 10;
			int b = 10;
			int c = a / b;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("计算出现异常，异常信息为：", e);
		}

		return JSON.toJSONString(user);
	}

	@GetMapping("/user1")
	public String user1(@RequestParam("username") String username) {
		logger.info("开始执行selectUserFromDB方法---------------------");
		User user = new User();
		user.setName(username);
		user.setAge(15);
		user.setSex("男");
		logger.info("selectUserFromDB查询用户信息为：" + JSON.toJSONString(user));
		try {
			int a = 10;
			int b = 10;
			int c = a / b;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("计算出现异常，异常信息为：", e);
		}
		return "33434343";
	}

	@ResponseBody
	@GetMapping(value = "/user2")
	public String user2(@RequestParam(value = "username") String username,
			@RequestParam(value = "password") String password) {
		User user = new User();
		user.setName(username);
		user.setPassword(password);
		return JSON.toJSONString(user);
	}

	@GetMapping("/user3/{usename}")
	@ResponseBody
	public String user3(@PathVariable(name = "usename", required = true) String usename) {
		User user = new User();
		user.setName(usename);
		return JSON.toJSONString(user);
	}

	@GetMapping("/user4/{usename}/{password}")
	@ResponseBody
	public String user5(@PathVariable(name = "usename", required = true) String usename,@PathVariable(name = "password", required = true) String password) {
		User user = new User();
		user.setName(usename);
		user.setPassword(password);
		user.setSex("男");
		return JSON.toJSONString(user);
	}
	
	@PostMapping("/user5")
	@ResponseBody
	public String user4(@RequestBody User user) {
		return JSON.toJSONString(user);
	}
	
	

	@PostMapping("/user6")
	public User user6(@RequestBody Map<String, Object> map) {
		User user = new User();
		String id = (String) map.get("id");
		String name = (String) map.get("name");
		Integer age = (Integer) map.get("age");
		List<String> hobby = (List<String>) map.get("hobby");
		user.setId(id);
		user.setAge(age);
		user.setName(name);
		user.setFriendList(hobby);
		return user;
	}
	@PostMapping("/user7")
	public User user7(@RequestBody List<String> cardIdList) {
		User user = new User();
		user.setFriendList(cardIdList);
		return user;
	}
	
	@PostMapping("/user8")
	public ProvinceEntity user8(@RequestBody ProvinceEntity provinceEntity) {
		
		return provinceEntity;
	}
	
	@PostMapping("/user9")
	public ProvinceEntity2 user9(@RequestBody ProvinceEntity2 provinceEntity2) {
		return provinceEntity2;
	}
	
	
	public static void main(String[] args) {
		/* 
		 * Map<String, Object> map = new HashMap<String, Object>(); List<String> hobby =
		 * new ArrayList<String>(); hobby.add("睡觉"); hobby.add("看电视"); hobby.add("工作");
		 * map.put("id", "1"); map.put("name", "莫平南"); map.put("age", "25");
		 * map.put("hobby", hobby); System.out.println(JSON.toJSON(map));
		 */
		
		List<String> cardIdList = new ArrayList<String>();
		cardIdList.add("erer");
		cardIdList.add("erer");
		cardIdList.add("erere");
		System.out.println(cardIdList);
		System.out.println(JSON.toJSON(cardIdList));
	}
}